package com.yuyan.openapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.yuyan.openapi.handler.RsaEncryptDecryptHandler;
import com.yuyan.openapi.handler.RsaSignatureHandler;
import com.yuyan.openapi.handler.SignatureHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author xm
 * @date 2024/1/24
 **/
@RestController
@Slf4j
public class CallbackController {

    /**
     * 签名处理器
     */
    private final static SignatureHandler SIGNATURE_HANDLER = new RsaSignatureHandler();
    /**
     * 加密解密处理器
     */
    private final static RsaEncryptDecryptHandler RSA_ENCRYPT_DECRYPT_HANDLER = new RsaEncryptDecryptHandler();

    @Value("${yuyan.mch_private}")
    public String mch_private;
    @Value("${yuyan.yuyan_common}")
    public String yuyan_common;

    private static final String success = "success";

    @PostMapping("pay_callback")
    public String pay_callback(@RequestBody Map<String, String> map) {
        log.info("宇燕支付回调：{}", JSONObject.toJSONString(map));
        String sign = map.get("sign");
        String data = map.get("data");
        // 用平台公钥解签
        boolean verify = SIGNATURE_HANDLER.verify(JSONObject.parseObject(data), sign, yuyan_common);
        if (!verify) {
            // todo 验签不通过
            log.info("宇燕支付回调验签不通过：{}", sign);
            throw new RuntimeException("验签失败");
        }
        // todo 对业务数据进行处理，请务必接入调用订单查询验证，以订单查询的信息为准！
        return success;
    }

    @PostMapping("refund_callback")
    public String refund_callback(@RequestBody Map<String, String> map) {
        log.info("宇燕退款回调：{}", JSONObject.toJSONString(map));
        String sign = map.get("sign");
        String data = map.get("data");
        // 用平台公钥解签
        boolean verify = SIGNATURE_HANDLER.verify(JSONObject.parseObject(data), sign, yuyan_common);
        if (!verify) {
            // todo 验签不通过
            log.info("宇燕支付回调验签不通过：{}", sign);
            throw new RuntimeException("验签失败");
        }
        // todo 对业务数据进行处理，请务必接入调用订单查询验证，以订单查询的信息为准！
        return success;
    }

    @PostMapping("withdraw_callback")
    public String withdraw_callback(@RequestBody Map<String, String> map) {
        log.info("宇燕钱包提现回调：{}", JSONObject.toJSONString(map));
        String sign = map.get("sign");
        String data = map.get("data");
        // 用平台公钥解签
        boolean verify = SIGNATURE_HANDLER.verify(JSONObject.parseObject(data), sign, yuyan_common);
        if (!verify) {
            // todo 验签不通过
            log.info("宇燕钱包提现回调验签不通过：{}", sign);
            throw new RuntimeException("验签失败");
        }
        // todo 对业务数据进行处理，请务必接入调用订单查询验证，以订单查询的信息为准！
        return success;
    }


    @PostMapping("transfer_callback")
    public String transfer_callback(@RequestBody Map<String, String> map) {
        log.info("宇燕钱包转账回调：{}", JSONObject.toJSONString(map));
        String sign = map.get("sign");
        String data = map.get("data");
        // 用平台公钥解签
        boolean verify = SIGNATURE_HANDLER.verify(JSONObject.parseObject(data), sign, yuyan_common);
        if (!verify) {
            // todo 验签不通过
            log.info("宇燕钱包转账回调验签不通过：{}", sign);
            throw new RuntimeException("验签失败");
        }
        // todo 对业务数据进行处理，请务必接入调用订单查询验证，以订单查询的信息为准！
        return success;
    }

    @PostMapping("div_callback")
    public String div_callback(@RequestBody Map<String, String> map) {
        log.info("宇燕分账回调：{}", JSONObject.toJSONString(map));
        String sign = map.get("sign");
        String data = map.get("data");
        // 用平台公钥解签
        boolean verify = SIGNATURE_HANDLER.verify(JSONObject.parseObject(data), sign, yuyan_common);
        if (!verify) {
            // todo 验签不通过
            log.info("宇燕分账回调验签不通过：{}", sign);
            throw new RuntimeException("验签失败");
        }
        // todo 对业务数据进行处理，请务必接入调用订单查询验证，以订单查询的信息为准！
        return success;
    }
}
